﻿using UnityEngine;
/// <summary>
/// U3D随机数的包装类 保持持续获取时不受其他干扰
/// 由于其为静态函数 在其他调用者使用时改变Seed 和 State 会导致上一调用者 后续随机变化
/// </summary>
public class U3DRandomWarp
{
	public int RandomSeed { get; set; }
	public Random.State CurState { get; set; }

	//public U3DRandomWarp()
	//{
	//	RandomSeed = Random.seed;
	//
	//}
	public U3DRandomWarp(int seed)
	{
		RandomSeed = seed;
		Random.InitState(seed);
		CurState = Random.state;
	}

	public Vector3 GetRandomPosition(Vector3 min, Vector3 max){
		SetRandom();
		Vector3 re = new Vector3(
			Random.Range(min.x, max.x),
			Random.Range(min.y, max.y),
			Random.Range(min.z, max.z)
			);
		SortRandom();
		return re;
	}
	public Vector3 GetRandomPosition(Vector2 min, Vector2 max)
	{
		SetRandom();
		Vector3 re = new Vector3(
			Random.Range(min.x, max.x),
			Random.Range(min.y, max.y),
			0
			);
		SortRandom();
		return re;
	}

	public float Range(float min, float max)
	{
		SetRandom();
		var re = Random.Range(min, max);
		SortRandom();
		return re;
	}


	protected void SetRandom()
	{
		Random.InitState(RandomSeed);
		Random.state = CurState;
	}
	protected void SortRandom()
	{
		CurState = Random.state;
	}
}
